﻿using CnBlogsClient.Interface;
using CnBlogsCore.Model;
using MvvmHelpers;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace CnBlogsClient.ViewModels
{
    class KbArticleDetailsViewModel : BaseViewModel
    {
        private KbArticleDetail articles;

        public KbArticleDetailsViewModel(KbArticleDetail kbArticles)
        {
            this.articles = kbArticles;
            //Title = articles.Title;
            
        }

        bool hasError;

        public bool HasError
        {
            get
            {
                return hasError;
            }

            set
            {
                SetProperty(ref hasError, value);
            }
        }
                                                                                                                                                                                                                 
        public async Task<KbArticleDetail> RefreshKbArticlesAsync()
        {
            try
            {
                IsBusy = true;
                HasError = false;

                string result = "";
                await Task.Run(() => {
                    result = DependencyService.Get<IKbArticleService>().GetKbArticlesDetail(articles.Id);
                });

                if (result.Length > 0)
                {
                    articles.Body = result;
                    HasError = false;
                }
                else
                {
                    HasError = true;
                } 
            }
            catch (Exception)
            {
                HasError = true;
            }
            finally
            {
                IsBusy = false;
            }

            return articles;
        }
    }
}
